package com.xiyou.luckdraw.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiyou.luckdraw.api.converter.UserExchangeConverter;
import com.xiyou.luckdraw.api.dal.entitys.UserExchange;
import com.xiyou.luckdraw.api.dal.mapper.luckdraw.UserExchange1Mapper;
import com.xiyou.luckdraw.api.dal.mapper.luckyelite.UserExchange2Mapper;
import com.xiyou.luckdraw.api.dto.order.OrderInfoDto;
import com.xiyou.luckdraw.api.dto.user.*;
import com.xiyou.luckdraw.api.service.IUserExchangeService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.exception.ExceptionProcessorUtils;
import com.xiyou.luckdraw.common.utils.DateUtils;
import com.xiyou.luckdraw.common.utils.ReqUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: UserExchangeServiceImpl
 * @Description: 用户兑换记录Service实现
 * @author: limingxing
 * @Date: 2019-10-08 12:47
 */
@Slf4j
@Service
public class UserExchangeServiceImpl implements IUserExchangeService {

    @Autowired
    private UserExchangeConverter exchangeConverter;

    @Autowired
    private UserExchange1Mapper userExchange1Mapper;
    @Autowired
    private UserExchange2Mapper userExchange2Mapper;

    @Override
    public ExchangeListResponse getExchangeList(ExchangeListRequest request) {
        log.error("Begin UserExchangeServiceImpl.getExchangeList request: {}",request);
        ExchangeListResponse response = new ExchangeListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            String datasourceType = ReqUtils.getDatasourceType();
            List<ExchangeListDto> exchangeList = null;
            if(StringUtils.isNotEmpty(request.getStartTime())){
                request.setStartTime(DateUtils.timeStamp2Date(request.getStartTime(),"YYYY-MM-dd"));
            }
            if(StringUtils.isNotEmpty(request.getEndTime())){
                request.setEndTime(DateUtils.timeStamp2Date(request.getEndTime(),"YYYY-MM-dd"));
            }
            if(StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)){
                exchangeList = userExchange1Mapper.getExchangeList(request);
            }else {
                exchangeList = userExchange2Mapper.getExchangeList(request);
            }
            PageInfo<ExchangeListDto> pageInfo = new PageInfo<>(exchangeList);

            response.setList(pageInfo.getList());
            response.setTotal(pageInfo.getTotal());
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());

        }catch (Exception e){
            log.error("UserExchangeServiceImpl.getExchangeList Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public UpdateExchangeResponse updateExchangeByStatus(UpdateExchangeRequest request) {
        log.error("Begin UserExchangeServiceImpl.updateExchangeByStatus request: {}",request);
        UpdateExchangeResponse response = new UpdateExchangeResponse();
        try{
            request.requestCheck();
            String datasourceType = ReqUtils.getDatasourceType();
            UserExchange exchange = new UserExchange();
            exchange.setId(request.getExchangeId());
            exchange.setStatus(request.getStatus());
            int row = 0;
            if(StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)){
                row = userExchange1Mapper.updateById(exchange);
            }else {
                row = userExchange2Mapper.updateById(exchange);
            }
            if(row>0){
                response.setCode(ResultCodeConstants.SUCCESS.getCode());
                response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
            }else{
                response.setCode(ResultCodeConstants.DATA_NOT_EXIST.getCode());
                response.setMsg(ResultCodeConstants.DATA_NOT_EXIST.getMessage());
            }
            log.info("UserExchangeServiceImpl.updateExchangeByStatus effect row : {}",row);
        }catch (Exception e){
            log.error("UserExchangeServiceImpl.updateExchangeByStatus Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
